<?php 
/*
 * 玩自拍静态类
 */
class Autodyne{
	/**
	 * 发表说话内容
	 * @param <int> $uin
	 * @param <string> $content
	 */
	static public function add($uin , $content , $time){
		$Db = loadDB();

		//把数据写入数组
		$insert_date = array(
			'uin' => $uin,
			'content' => $content,
			'send_uin' => 0,
			'reply' => '',
			'time' => $time,
		);
		$success = $Db->doInsert('autodyne' , $insert_date);
		return $success ? 0 : 751;
	}
	/**
	 * 获取总数
	 * @param <int> $uin
	 */
	static public function getCount() {
		$Db = loadDB();
		
		$result= $Db->getOne("SELECT COUNT(*) AS `count` From `autodyne`");

		return empty($result) ? 0 : $result['count'];
	}
	
	/**
	 * 获取内容列表
	 * @param <int> $pageSize
	 * @param <int> $pageCount
	 */
	static public function getList($pageSize, $pageCount) {
		$Db = loadDB();
		//$pageSize=1;
		//$pageCount=10;
		$pageSize=($pageSize-1)*$pageCount;
		$list = $Db->getAll("SELECT  * FROM `autodyne` ORDER BY time DESC LIMIT {$pageSize} , {$pageCount}");
		
		//解析回复
		foreach($list as $k => $row) {
			$list[$k]['reply'] = empty($row['reply']) ? array() : unserialize($row['reply']);
		}
		
		return $list;
	}
	/**
	 * 获取单条玩自拍
	 * @param <int> $id
	 * 
	 */
	static public function getSend($id , $uin) {
		$Db = loadDB();
		
		$list = $Db->getOne("SELECT  * FROM `autodyne` WHERE autodyne.id = {$id} AND autodyne.uin = {$uin}");
		
		//解析单条回复
		
		$list['reply'] = empty($list['reply']) ? array() : unserialize($list['reply']);
		
		//print_r($list);
		return $list;
	}
	/**
	 * 快速回复
	 * @param <int> $uin
	 * @param <int> $id
	 * @param <string> $content
	 */
	static public function getUpSend($uin , $owner_uin , $id , $content,$org_autodyne = false) {
		$Db = loadDB();
	//	print_r($owner_uin);
		//print_r($id);
		// 取原数据
		if(empty($org_autodyne)) {
			$amorism = self::getSend($id , $owner_uin);
			//print_r($amorism);
			if(empty($autodyne)) {
				return 754;	// 该信息不存在
			}
		} else {
			$autodyne = $org_autodyne;
		}
	//	print_r($amorism);
		$time = time();
		$autodyne['reply'][] = array('uin' => $uin , 'content' => $content , 'time' => $time);
		
		// 写入数据
		$update_data = array(
			'send_uin' => $uin,
			'reply' => serialize($autodyne['reply']),
		);
		$success = $Db->doUpdate('autodyne' , $update_data , '`uin`= '.$owner_uin.' AND `id` = ' . $id);

		return $success ? 0 : 752;
	}
	/**
	 * 删除谈话内容
	 * @param <int> $id
	 */
	static public function delete($id , $uin){
		$Db=loadDB();
		
		//删除投诉信息
		$success =$Db->query("DELETE FROM `autodyne` WHERE `id` = {$id} AND `uin` = {$uin}");
		return $success ? 0 : 753;
	}
	/**
	 * 时间格式转换,拼接图片路径
	 */
	static public function getDate($uin , $time){
		
		$date = date("Y/md",$time);
		$path='autodyne/'. $date . '/' . $uin . '_' . $time . '.jpg';
		return $path;
	}
}


# end of script